// pages/ucenter/order/order.js
var UTILS = require("../../../utils/util.js");
var API = require("../../../config/api.js");

var APP = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    RESOURCE_ROOT: "",
    pages: {
      detail: "/pages/ucenter/order/detail/detail"
    },
    ordersNoPay: [],
    ordersWaiting: [],
    ordersHistory: []
  },
  /**
   * 自定义函数
   */
  onClickTabs: function (event) {
    var tabIndex = event.detail.index;
    if (tabIndex === 2) {
      this.getOrdersHistoryFromServer();
    }
  },
  onClickLeft: function () {
    console.log("onclick");
    wx.navigateBack({
      delta: 1, // 回退前 delta(默认为1) 页面
      success: function(res){}
    });
  },
  onChange: function () {

  },
  toOrderDetail: function (event) {
    var orderId = event.currentTarget.dataset.orderid;
    wx.navigateTo({
      url: "./detail/detail?orderId=" + orderId
    });
  },
  toPage: function (e) {
    let page = e.currentTarget.dataset.page;
    if (page !== undefined) {
      if (this.data.isLogined && page === this.data.pages.login) {
        return;
      }
      wx.navigateTo({
        url: page,
      });
    }
  },
  getOrdersNoPayFromServer: function () {
    var _this = this;
    var userId = APP.globalData.userInfo.id;
    var urlNoPay = API.GET_ORDER_LIST + "/" + userId + "/" + UTILS.ORDER_NOPAY;
    var header = {
      Authorization: APP.globalData.token,
      userId: APP.globalData.userInfo.id
    };
    UTILS.httpGet(urlNoPay, header, function(data) {
      for (let index in data.result) {
        let orderTime = UTILS.formatTime(data.result[index].gmtModified.epochSecond, "h:m");
        _this.setData({
          ordersNoPay: data.result,
          [`ordersNoPay[${index}].orderTime`]: orderTime
        });
      }
    });
  },
  getOrdersWaitingFromServer: function () {
    var _this = this;
    var userId = APP.globalData.userInfo.id;
    var urlWaiting = API.GET_ORDER_LIST + "/" + userId + "/" + UTILS.ORDER_WAIT;
    var header = {
      Authorization: APP.globalData.token,
      userId: APP.globalData.userInfo.id
    };
    UTILS.httpGet(urlWaiting, header, function(data) {
      for (let index in data.result) {
        let orderTime = UTILS.formatTime(data.result[index].gmtModified.epochSecond, "h:m");
        _this.setData({
          ordersWaiting: data.result,
          [`ordersWaiting[${index}].orderTime`]: orderTime
        });
      }
    });
  },
  getOrdersHistoryFromServer: function () {
    var _this = this;
    var userId = APP.globalData.userInfo.id;
    var urlHistory = API.GET_ORDER_LIST + "/" + userId + "/" + UTILS.ORDER_FINISH;
    var header = {
      Authorization: APP.globalData.token,
      userId: APP.globalData.userInfo.id
    };
    UTILS.httpGet(urlHistory, header, function(data) {
      for (let index in data.result) {
        let orderTime = UTILS.formatTime(data.result[index].gmtModified.epochSecond, "Y M D h:m");
        _this.setData({
          ordersHistory: data.result,
          [`ordersHistory[${index}].orderTime`]: orderTime
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({
      RESOURCE_ROOT: API.RESOURCE_ROOT
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getOrdersNoPayFromServer();
    this.getOrdersWaitingFromServer();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})